﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;


namespace Utility
{
    public class PrintReportHelper
    {
        /// <summary>
        /// 综合查询中生成报表
        /// </summary>
        /// <param name="title"></param>
        /// <param name="gv"></param>
        /// <param name="pdfSavePath"></param>
        /// <param name="cellWidths"></param>
        /// <returns></returns>
        public static bool printStatisticsReport(String title, GridView gv, String pdfSavePath, int[] cellWidths, String statisticsInfo)
        {
            bool result = false;
            int columnsCount = gv.Columns.Count;
            int rowsCount = gv.Rows.Count;

            Document myDocument = new Document(PageSize.A4, 50, 50, 50, 50);
            try
            {
                PdfWriter.GetInstance(myDocument, new FileStream(pdfSavePath, FileMode.Create));
                myDocument.Open();
                string fontPath = Environment.GetEnvironmentVariable("WINDIR") + "\\FONTS\\SIMHEI.TTF";//强制中文字体，否则无法显示中文
                string ServerPathkai = Environment.GetEnvironmentVariable("WINDIR") + "\\FONTS\\SIMKAI.TTF";
                BaseFont baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                BaseFont baseFontKai = BaseFont.CreateFont(ServerPathkai, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//楷体

                //标题居中
                Paragraph paragraph = new Paragraph(title + "\n\n", new Font(baseFontKai, 20));
                paragraph.Alignment = Element.ALIGN_CENTER;
                myDocument.Add(paragraph);

                List list = new iTextSharp.text.List(true, 20);


                //添加一个表格
                PdfPTable pdfPTable = new PdfPTable(columnsCount);
                pdfPTable.TotalWidth = myDocument.Right - myDocument.Left;
                //float[] widths = { 80f, 130f, 50f, 30f, 50f, 50f};
                pdfPTable.SetWidths(cellWidths);
                pdfPTable.LockedWidth = true;                //必须锁定宽度，否则修改无效




                //增加表头
                PdfPCell pdfPCell;
                for (int i = 0; i < columnsCount; i++)
                {
                    pdfPCell = new PdfPCell(new Phrase(gv.Columns[i].HeaderText, new Font(baseFont, 9)));
                    pdfPCell.HorizontalAlignment = Element.ALIGN_CENTER;
                    pdfPCell.VerticalAlignment = Element.ALIGN_CENTER;
                    pdfPTable.AddCell(pdfPCell);
                }

                //增加详细数据
                for (int i = 0; i < rowsCount; i++)
                {
                    for (int j = 0; j < columnsCount; j++)
                    {
                        pdfPCell = new PdfPCell(new Phrase(gv.Rows[i].Cells[j].Text.ToString(), new Font(baseFont, 9)));
                        pdfPCell.HorizontalAlignment = Element.ALIGN_CENTER;
                        pdfPCell.VerticalAlignment = Element.ALIGN_CENTER;
                        pdfPTable.AddCell(pdfPCell);
                    }
                }

                myDocument.Add(pdfPTable);


                paragraph = new Paragraph(statisticsInfo, new Font(baseFont, 9));
                paragraph.Alignment = Element.ALIGN_LEFT;
                myDocument.Add(paragraph);




                myDocument.Close();

                try  //测试用
                {
                    System.Diagnostics.Process.Start(pdfSavePath);
                }
                catch (Exception ex)
                {

                }
            }
            catch (Exception ex)
            {
            }
            return result;
        }

    }
}
